ABC260 C - Changing Jewels
提出
code: python
n, x, y = list(map(int, input().split()))
# Rn * 1
# Rn => Rn-1 + Bn * X
# Bn => Rn-1 + Bn-1 * Y
# Rn => Rn-1 + (Rn-1 + Bn-1 * Y) * X => Rn-1 * (X + 1) + Bn-1 * (X * Y)
# 2 3 4
def exchange(level, rnum):
if level == 2:
return rnum * x * y
elif level == 1:
return 0
else:
return exchange(level - 1, x+1)
print(exchange(n, 1))
解答
code: python
n, x, y = list(map(int, input().split()))
# Rn => Rn-1 + Bn * X
# Bn => Rn-1 + Bn-1 * Y
def exchange(level, is_red):
if level == 1:
return 0 if is_red else 1
if is_red:
return exchange(level - 1, True) + exchange(level, False) * x
else:
return exchange(level - 1, True) + exchange(level - 1, False) * y
print(exchange(n, True))
メモ
提出
TLE
code: python
n, x, y = list(map(int, input().split()))
# R(N) -> R(N-1) + B(N) * X
# B(N) -> R(N-1) + B(N-1) * Y
# R(N) -> (X+1) R(N-1) + XY B(N-1)
# 2 3 4
# R(2) -> 4 R(1) + 12 B(1)
def red(level, num):
if level == 1:
return
for _ in range(num):
red(level-1, x+1)
blue(level-1, x*y)
def blue(level, num):
if level == 1:
return
for _ in range(num):
red(level-1, 1)
blue(level-1, y)
red(n, 1)